package com.gem.controller;

import com.gem.domain.*;
import com.gem.service.CampusService;
import com.gem.service.CoreService;
import com.gem.util.MessageUtil;
import com.gem.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author: 玲
 * @Description:
 * @create 2019-10-21 21:17
 * @Modified By:
 */
@Controller
@SuppressWarnings("all")
public class CoreController {

    @Autowired
    private CoreService service;

    @Autowired
    private CampusService campusService;

    @RequestMapping("/coreEvent/findEventByCampusId")
    public String findEventByCampusId(Integer campusId, Model model,
                                      @RequestParam("RcurrentPage") int RcurrentPage,
                                      @RequestParam("RpageSize") int RpageSize,
                                      @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                                      @RequestParam(value = "pageSize", defaultValue = "7") int pageSize) {
        model.addAttribute("RcurrentPage", RcurrentPage);
        model.addAttribute("RpageSize", RpageSize);
        try {
            Campus campus = service.findCampus(campusId);
            model.addAttribute("campus", campus);
            PageInfo<CoreEvent> pageBeans = service.findEventById(campusId, currentPage, pageSize);
            if (pageBeans != null && pageBeans.getList().size() > 0) {
                model.addAttribute("pageBean", pageBeans);
                List<Integer> pageValue = PageUtil.getPageValue(pageBeans.getPages(), currentPage);
                model.addAttribute("pageValue", pageValue);
            } else {
                model.addAttribute("info", "没有数据请先添加！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/campus/infoFindAll", "服务器异常！请联系管理员！", model);
            return "message";
        }
        return "campus/core_eventList";
    }

    @RequestMapping("/coreEvent/saveUI")
    public String saveUI(Integer campusId, Model model,
                         @RequestParam(value = "RcurrentPage", defaultValue = "1") int RcurrentPage,
                         @RequestParam(value = "RpageSize", defaultValue = "5") int RpageSize) {
        model.addAttribute("RcurrentPage", RcurrentPage);
        model.addAttribute("RpageSize", RpageSize);
        try {
            Campus campus = campusService.findById(campusId);
            model.addAttribute("campus", campus);
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/coreEvent/findByGid?campusId=" + campusId +
                    "&RcurrentPage=" + RcurrentPage + "&RpageSize=" + RpageSize, "服务器异常！请联系管理员！", model);
            return "message";
        }
        return "campus/info_event_insert";
    }

    @RequestMapping("/coreEvent/save")
    public String save(CorePlan corePlan, Model model,
                       @RequestParam("RcurrentPage") int RcurrentPage,
                       @RequestParam("RpageSize") int RpageSize) {
        model.addAttribute("RcurrentPage", RcurrentPage);
        model.addAttribute("RpageSize", RpageSize);
        try {
            service.savePlan(corePlan, corePlan.getCampusId());
            MessageUtil.returnInfoPage("3", "/coreEvent/findGroup?campusId=" + corePlan.getCampusId() +
                    "&RcurrentPage=" + RcurrentPage + "&RpageSize=" + RpageSize, "添加成功！", model);

        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/coreEvent/findByGid?campusId=" + corePlan.getCampusId() +
                    "&RcurrentPage=" + RcurrentPage + "&RpageSize=" + RpageSize, "服务器异常！请联系管理员！", model);

        }
        return "message";
    }

    @RequestMapping("/coreEvent/updateUI")
    public String updateUI(Integer coreEventId, Integer campusId, Model model, String Gid,
                           @RequestParam("RcurrentPage") int RcurrentPage,
                           @RequestParam("RpageSize") int RpageSize) {
        model.addAttribute("RcurrentPage", RcurrentPage);
        model.addAttribute("RpageSize", RpageSize);
        model.addAttribute("Gid", Gid);
        try {
            CoreEvent coreEvent = service.findById(coreEventId);
            model.addAttribute("coreEvent", coreEvent);
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/coreEvent/findByGid?campusId=" + campusId +
                    "&RcurrentPage=" + RcurrentPage + "&RpageSize=" + RpageSize + "&Gid=" + Gid, "服务器异常！请联系管理员！", model);
            return "message";
        }
        return "campus/info_event_update";
    }

    @RequestMapping("/coreEvent/update")
    public String update(CoreEvent coreEvent, Model model, String Gid,
                         @RequestParam("RcurrentPage") int RcurrentPage,
                         @RequestParam("RpageSize") int RpageSize) {
        model.addAttribute("RcurrentPage", RcurrentPage);
        model.addAttribute("RpageSize", RpageSize);
        try {
            service.updateComplete(coreEvent);
            MessageUtil.returnInfoPage("3", "/coreEvent/findByGid?campusId=" + coreEvent.getCampusId() +
                    "&RcurrentPage=" + RcurrentPage + "&RpageSize=" + RpageSize + "&Gid=" + Gid, "修改成功！", model);
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/coreEvent/findByGid?campusId=" + coreEvent.getCampusId() +
                    "&RcurrentPage=" + RcurrentPage + "&RpageSize=" + RpageSize + "&Gid=" + Gid, "服务器异常！请联系管理员！", model);
        }
        return "message";
    }

    @RequestMapping("/coreEvent/delete")
    @ResponseBody
    public String delete(Integer coreEventId){
        try {
            service.deleteById(coreEventId);
            return "1";
        } catch (Exception e) {
            e.printStackTrace();
            return "-1";
        }
    }


    @RequestMapping("/coreEvent/deleteAll")
    public String deleteAll(int[] coreEventIds, Integer campusId, Model model, String Gid,
                            @RequestParam("RcurrentPage") int RcurrentPage,
                            @RequestParam("RpageSize") int RpageSize) {
        model.addAttribute("RcurrentPage", RcurrentPage);
        model.addAttribute("RpageSize", RpageSize);
        try {
            if (coreEventIds != null && coreEventIds.length != 0) {
                service.deleteAll(coreEventIds);
                MessageUtil.returnInfoPage("3", "/coreEvent/findByGid?campusId=" + campusId +
                        "&RcurrentPage=" + RcurrentPage + "&RpageSize=" + RpageSize + "&Gid=" + Gid, "删除成功!", model);
            } else {
                MessageUtil.returnInfoPage("3", "/coreEvent/findByGid?campusId=" + campusId +
                        "&RcurrentPage=" + RcurrentPage + "&RpageSize=" + RpageSize + "&Gid=" + Gid, "没有参数传递", model);
            }
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/coreEvent/findByGid?campusId=" + campusId +
                    "&RcurrentPage=" + RcurrentPage + "&RpageSize=" + RpageSize + "&Gid=" + Gid, "服务器异常！请联系管理员", model);
        }

        return "message";
    }

    @RequestMapping("/coreEvent/findGroup")
    public String findGroup(Integer campusId, Model model,
                            @RequestParam(value = "RcurrentPage", defaultValue = "1") int RcurrentPage,
                            @RequestParam(value = "RpageSize", defaultValue = "5") int RpageSize,
                            @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                            @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        model.addAttribute("RcurrentPage", RcurrentPage);
        model.addAttribute("RpageSize", RpageSize);
        try {
            Campus campus = service.findCampus(campusId);
            model.addAttribute("campus", campus);
            PageInfo<String> pageBeans = service.findGroup(campusId, currentPage, pageSize);
            if (pageBeans != null && pageBeans.getList().size() > 0) {
                model.addAttribute("pageBean", pageBeans);
                List<Integer> pageValue = PageUtil.getPageValue(pageBeans.getPages(), currentPage);
                model.addAttribute("pageValue", pageValue);
            } else {
                model.addAttribute("info", "没有数据请先添加！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/campus/infoFindAll", "服务器异常！请联系管理员！", model);
            return "message";
        }
        return "campus/info_event";
    }

    @RequestMapping("/coreEvent/findByGid")
    public String findByGid(String Gid, Model model, Integer campusId,
                            @RequestParam(value = "RcurrentPage", defaultValue = "1") int RcurrentPage,
                            @RequestParam(value = "RpageSize", defaultValue = "5") int RpageSize) {
        model.addAttribute("RcurrentPage", RcurrentPage);
        model.addAttribute("RpageSize", RpageSize);
        model.addAttribute("campusId", campusId);
        model.addAttribute("Gid", Gid);
        try {
            PageInfo<CoreEvent> pageBeans = service.findByGid(Gid, 1, 7);
            if (pageBeans != null && pageBeans.getList().size() > 0) {
                model.addAttribute("pageBean", pageBeans);
                List<Integer> pageValue = PageUtil.getPageValue(pageBeans.getPages(), 1);
                model.addAttribute("pageValue", pageValue);
            } else {
                model.addAttribute("info", "没有数据请先添加！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/campus/infoFindAll", "服务器异常！请联系管理员！", model);
            return "message";
        }
        return "campus/core_eventList";
    }

    @RequestMapping("/coreEvent/deleteByGid")
    @ResponseBody
    public String deleteByGid(String Gid,int currentPageSize, int currentPage, int lastPage){
        try {
            if(service.deleteByGid(Gid)>0){
                if (currentPageSize == 1 && currentPage == lastPage) {
                    return "0";
                }
            }
            return "1";
        } catch (Exception e) {
            e.printStackTrace();
            return "-1";
        }
    }

    @RequestMapping("/coreEvent/deleteGAll")
    public String deleteGAll(String Gids[], Model model, Integer campusId,
                             @RequestParam(value = "RcurrentPage", defaultValue = "1") int RcurrentPage,
                             @RequestParam(value = "RpageSize", defaultValue = "5") int RpageSize) {
        try {
            if (Gids != null && Gids.length != 0) {
                service.deleteGAll(Gids);
                MessageUtil.returnInfoPage("3", "/coreEvent/findGroup?campusId=" + campusId +
                        "&RcurrentPage=" + RcurrentPage + "&RpageSize=" + RpageSize, "删除成功!", model);
            } else {
                MessageUtil.returnInfoPage("3", "/coreEvent/findGroup?campusId=" + campusId +
                        "&RcurrentPage=" + RcurrentPage + "&RpageSize=" + RpageSize, "没有传递参数!", model);
            }
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/campus/infoFindAll", "服务器异常！请联系管理员!", model);
        }
        return "message";
    }
}
